package com.loong.x.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.core.io.ClassPathResource;

import java.io.*;
import java.nio.charset.StandardCharsets;
/**
 * Description
 * 加载.json文件 解析返回json对象或字符串
 *
 * @author wcloong
 * @version 0.1.0
 * @since 2023/6/27 16:21
 */
public class LoadJSONFile {


    public String jsonPath(String name) {
        if ("方法一".equals("方法1")) {
            String property = System.getProperty("user.dir");
            return property + "/src/main/resources/json/" + name + ".json";
        }
        if ("方法2".equals("方法2")) {
            ClassPathResource classPathResource = new ClassPathResource("/json/"+name+".json");
            try {
                InputStream inputStream =classPathResource.getInputStream();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    //读取json文件
    //通过文件读取获取流的方式行不通，因为无法直接读取压缩包中的文件，读取只能通过流的方式读取
    public String readJsonFile(String Filename /*传文件的路径*/) {
        String jsonStr = "";
        try {
            File jsonFile = new File(Filename);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile), StandardCharsets.UTF_8);
            int ch;
            StringBuilder sb = new StringBuilder();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
//            System.out.println(jsonStr.getClass().getName());
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    public JSONObject getLoadJson(String name) {
        String strPath = jsonPath(name);
        System.out.println("file path : " + strPath);
        String s = readJsonFile(strPath);
        System.out.println("json : \n" + PrettyJSON.pretty(s));
        return (JSONObject) JSON.parse(s);
    }
    //通过类加载器读取文件流，类加载器可以读取jar包中的编译后的class文件，当然也是可以读取jar包中的文件流
    public static JSONObject readJsonFile1(String name){
        ClassPathResource classPathResource = new ClassPathResource("/json/"+name+".json");
        try {
            InputStream inputStream =classPathResource.getInputStream();
            Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            int ch;
            StringBuilder sb = new StringBuilder();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            reader.close();
            System.out.println("json : \n" + PrettyJSON.pretty(sb.toString()));
            return (JSONObject) JSON.parse(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
